home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gpt32src.zip / DOC2TEX.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  5KB  |  251 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: doc2tex.c,v 3.26 1992/03/25 04:53:29 woo Exp woo $";
  3. #endif
  4.  
  5. /*
  6.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  7.  * Also will work for VMS .HLP files. 
  8.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  9.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  10.  *
  11.  * usage:  doc2tex < file.doc > file.tex
  12.  *
  13.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  14.  *     article document suitable for printing with LaTeX.
  15.  *
  16.  * typical usage for GNUPLOT:
  17.  *
  18.  *   doc2tex < gnuplot.doc > gnuplot.tex 
  19.  *   latex gnuplot.tex ; latex gnuplot.tex
  20.  */
  21.  
  22. static char rcsid[] = "$Id: doc2tex.c,v 3.26 1992/03/25 04:53:29 woo Exp woo $";
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #ifdef AMIGA_LC_5_1
  27. #include <string.h>
  28. #endif
  29.  
  30. #define MAX_NAME_LEN    256
  31. #define MAX_LINE_LEN    256
  32. #define TRUE 1
  33. #define FALSE 0
  34.  
  35. typedef int boolean;
  36.  
  37. boolean intable = FALSE;
  38. boolean verb = FALSE;
  39.  
  40. main()
  41. {
  42.     init(stdout);
  43.     convert(stdin,stdout);
  44.     finish(stdout);
  45.     exit(0);
  46. }
  47.  
  48.  
  49. init(b)
  50. FILE *b;
  51. {
  52.     (void) fputs("\\input{titlepage.tex}\n",b);
  53. }
  54.  
  55.  
  56. convert(a,b)
  57.     FILE *a,*b;
  58. {
  59.     static char line[MAX_LINE_LEN];
  60.  
  61.     while (fgets(line,MAX_LINE_LEN,a)) {
  62.        process_line(line, b);
  63.     }
  64. }
  65.  
  66. process_line(line, b)
  67.     char *line;
  68.     FILE *b;
  69. {
  70.     switch(line[0]) {        /* control character */
  71.        case '?': {            /* interactive help entry */
  72.           break;            /* ignore */
  73.        }
  74.        case '@': {            /* start/end table */
  75.           if (intable) {
  76.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  77.              (void) fputs("\\end{center}\n",b);
  78.              intable = FALSE;
  79.           } else {
  80.              if (verb) {
  81.                 (void) fputs("\\end{verbatim}\n",b);
  82.                 verb=FALSE;
  83.              } 
  84.              (void) fputs("\n\\begin{center}\n", b);
  85.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  86.              intable = TRUE;
  87.           }
  88.           /* ignore rest of line */
  89.           break;
  90.        }
  91.        case '#': {            /* latex table entry */
  92.           if (intable)
  93.             (void) fputs(line+1, b); /* copy directly */
  94.           else
  95.             fprintf(stderr, "error: # line found outside of table\n");
  96.           break;
  97.        }
  98.        case '%': {            /* troff table entry */
  99.           break;            /* ignore */
  100.        }
  101.        case '\n':            /* empty text line */
  102.        case ' ': {            /* normal text line */
  103.           if (intable)
  104.             break;        /* ignore while in table */
  105.           if (line[1] == ' ') {
  106.              /* verbatim mode */
  107.              if (!verb) {
  108.                 (void) fputs("\\begin{verbatim}\n",b);
  109.                 verb=TRUE;
  110.              }
  111.              (void) fputs(line+1,b); 
  112.           } else {
  113.              if (verb) {
  114.                 (void) fputs("\\end{verbatim}\n",b);
  115.                 verb=FALSE;
  116.              } 
  117.              if (line[0] == '\n')
  118.                puttex(line,b); /* handle totally blank line */
  119.              else
  120.                puttex(line+1,b);
  121.           }
  122.           break;
  123.        }
  124.        default: {
  125.           if (isdigit(line[0])) { /* start of section */
  126.              if (!intable)    /* ignore while in table */
  127.                section(line, b);
  128.           } else
  129.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  130.                   line[0]);
  131.           break;
  132.        }
  133.     }
  134. }
  135.  
  136. /* process a line with a digit control char */
  137. /* starts a new [sub]section */
  138.  
  139. section(line, b)
  140.     char *line;
  141.     FILE *b;
  142. {
  143.     static char string[MAX_LINE_LEN];
  144.     int sh_i;
  145.  
  146.     if (verb) {
  147.        (void) fputs("\\end{verbatim}\n",b);
  148.        verb=FALSE;
  149.     } 
  150. #ifdef AMIGA_LC_5_1
  151.     (void) sscanf(line,"%d",&sh_i);
  152.     strcpy(string,strchr(line,' ')+1);
  153.     {
  154.       char *p;
  155.       p = strchr(string,'\n');
  156.       if (p != NULL) *p = '\0';
  157.     }
  158. #else
  159.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  160. #endif
  161.     switch(sh_i)
  162.      {
  163.         case 1: 
  164.         (void) fprintf(b,"\\section{");
  165.         break;
  166.         case 2: 
  167.         (void) fprintf(b,"\\section{");
  168.         break;
  169.         case 3:
  170.         (void) fprintf(b,"\\subsection{");
  171.         break;
  172.         case 4: 
  173.         (void) fprintf(b,"\\subsubsection{");
  174.         break;
  175.         default:
  176.         case 5: 
  177.         (void) fprintf(b,"\\paragraph{");
  178.         break;
  179.      }
  180.     if (islower(string[0]))
  181.      string[0] = toupper(string[0]);
  182.     puttex(string,b);
  183.     (void) fprintf(b,"}\n");
  184. }
  185.  
  186. /* put text in string str to file while buffering special TeX characters */
  187. puttex(str,file)
  188. FILE *file;
  189. register char *str;
  190. {
  191. register char ch;
  192. static boolean inquote = FALSE;
  193.  
  194.      while( (ch = *str++) != '\0') {
  195.          switch(ch) {
  196.              case '#':
  197.              case '$':
  198.              case '%':
  199.              case '&':
  200.              case '_':
  201.              case '{':
  202.              case '}':
  203.                  (void) fputc('\\',file);
  204.                  (void) fputc(ch,file);
  205.                  break;
  206.              case '\\':
  207.                  (void) fputs("$\\backslash$",file);
  208.                  break;
  209.              case '~':
  210.                  (void) fputs("\\~{\\ }",file);
  211.                  break;
  212.              case '^':
  213.                  (void) fputs("\\verb+^+",file);
  214.                  break;
  215.              case '>':
  216.              case '<':
  217.              case '|':
  218.                  (void) fputc('$',file);
  219.                  (void) fputc(ch,file);
  220.                  (void) fputc('$',file);
  221.                  break;
  222.              case '"': 
  223.                  /* peek at next character: if space, end of quote */
  224.                  if (*str == NULL || isspace(*str) || ispunct(*str))
  225.                    (void) fputs("''", file);
  226.                  else
  227.                    (void) fputs("``", file);
  228.                  break;
  229.              case '`':    /* backquotes mean boldface */
  230.                  if (inquote) {
  231.                     fputs("}", file);
  232.                     inquote = FALSE;
  233.                  } else {
  234.                     fputs("{\\bf ", file);
  235.                     inquote = TRUE;
  236.                  }
  237.                  break;
  238.              default:
  239.                  (void) fputc(ch,file);
  240.                  break;
  241.          }
  242.      }
  243. }
  244.  
  245.  
  246. finish(b)
  247. FILE *b;
  248. {
  249.     (void) fputs("\\end{document}\n",b);
  250. }
  251.